export const types = {
  doc: ['doc', 'docx'],
  ppt: ['ppt', 'pptx'],
  xls: ['xls', 'xlsx'],
  video: ['mp4'],
  audio: ['mp3', 'wav', 'ogg'],
  txt: ['txt'],
  pdf: ['pdf'],
  zip: ['zip', 'rar'],
  json: ['json'],
  md: ['md'],
  img: ['jpg', 'jpeg', 'gif', 'png', 'bmp']
}
/* 过滤掉删除的级联选项 */
export function filterOptions (options) {
  return options.filter((item) => {
    if (item.isDelete !== 1) {
      const children = filterOptions(item.children || []);
      item.children = children.length ? children : null;
      return true;
    }
  });
}
// 省市县 一级，二级，三级深度处理
export function deepRegion (options, deep = 1, regionType) {
  return options.map(({ name, children, fullname, id }) => {
    const item: any = { name, fullname, id }
    if (deep === regionType || children === null) {
      item.children = null
    } else {
      item.children = deepRegion(children, deep + 1, regionType)
    }
    return item
  })
}
export function isUndef (v: any): boolean {
  return v === undefined || v === null
}
export function getFormatDate (date: any, type?: string) {
  if (typeof date === 'string') {
    date = new Date(date)
  }
  const year = date.getFullYear()
  let month = date.getMonth()
  let day = date.getDate()
  month = month + 1 > 9 ? month + 1 : '0' + (month + 1)
  day = day > 9 ? day : '0' + day
  if (type === 'date') {
    return year + '-' + month + '-' + day
  }
  let hour = date.getHours()
  let minites = date.getMinutes()
  let second = date.getSeconds()
  hour = hour > 9 ? hour : '0' + hour
  minites = minites > 9 ? minites : '0' + minites
  second = second > 9 ? second : '0' + second
  return year + '-' + month + '-' + day + ' ' + hour + ':' + minites + ':' + second
}

export function formatShortTime (date: string) {
  if (!date) {
    return ''
  } else {
    const d = date.substring(0, 10) // 2020-07-07
    const t = date.substring(11, 16)
    const a = d.split('-') // ['年', '月', '日']
    const currYear = new Date().getFullYear() + '' // 当前时间的年
    const currMonth = (new Date().getMonth() + 1) > 9 ? '' + (new Date().getMonth() + 1) : '0' + (new Date().getMonth() + 1)
    const currDate = new Date().getDate() > 9 ? '' + new Date().getDate() : '0' + new Date().getDate()
    const currentTime = new Date().getTime()
    const dday = new Date(date).getTime()
    const diff = dday - currentTime
    const day = Math.abs(diff / 1000 / 3600 / 24) // 计算日期
    const hour = Math.abs(diff / 1000 / 3600) // 计算小时
    const minutes = Math.abs(diff / 1000 / 60) // 计算分钟
    const seconds = Math.abs(diff / 1000) // 计算秒

    if (currYear !== a[0]) { // 如果传入的日期的年份不等于当前日期的年份 那么直接返回date
      return date
    } else { // 如果年份相等 判定日期的月份是否相等
      if (currMonth !== a[1]) { // 如果年相等，月不相等 直接返回月 日 和时间
        return `${a[1]}-${a[2]} ${t}`
      } else { // 如果年相等，月相等
        if (currDate !== a[2]) { // 如果日不相等
          if (Number(currDate) - Number(a[2]) === 1) { // 如果传入进来的日期等于昨天 显示昨天 否则 显示 月 日 加时间
            return '昨天'
          } else {
            return `${a[1]}-${a[2]} ${t}`
          }
        } else { // 如果日相等
          if (seconds > 0 && seconds < 60) {
            if (seconds < 1) {
              return '刚刚'
            }
            return parseInt(seconds + '', 10) + '秒前'
          } else if (minutes > 0 && minutes < 60) {
            return `${parseInt(minutes + '', 10)}分钟前`
            return `今日${t}`
          }
        }
      }
    }
  }
  return ''
}
